package com.ssh.repository.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ssh.entity.Role;
import com.ssh.repository.RoleRepository;

@Repository
public class RoleReqositoryImpl implements RoleRepository {
	
	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession(){
		return this.sessionFactory.openSession();
	}

	@Override
	public Role load(Long id) {
		return (Role) getCurrentSession().load(Role.class, id);
	}

	@Override
	public Role get(Long id) {
		return (Role) getCurrentSession().get(Role.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> findAll() {
		return (List<Role>) getCurrentSession().get(Role.class, null);
	}

	@Override
	public void persist(Role entity) {
		getCurrentSession().persist(entity);
	}

	@Override
	public Long save(Role entity) {
		return (Long) getCurrentSession().save(entity);
	}

	@Override
	public void saveOrUpdate(Role entity) {
		getCurrentSession().saveOrUpdate(entity);
	}

	@Override
	public void delete(Long id) {
		Role role = load(id);
		getCurrentSession().delete(role);
	}

	@Override
	public void flush() {
		getCurrentSession().flush();
	}
}
